package controllers

import (
	"github.com/kataras/iris/v12"
	"irisfarm/app/services"
	"irisfarm/app/validates"
)

type UserController struct {
	BaseController
}

func NewUserController(ctx iris.Context) *UserController {
	c := new(UserController)
	c.InitContext(ctx)
	return c
}

// Login
// @api {get} /api/user/login  登录
//
// @apiGroup LiveGroup
//
// @apiParam {string}  username  用户名
// @apiParam {string}  password  密码
//
// @apiVersion 1.0.0
// @apiSuccessExample {json} Success-Response:
// 	  HTTP/1.1 200 OK
// 	  {
//  	"status_code": 200,
//  	"message": "成功",
//  	"data": {
// 			"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJcdTAwMDEifQ.UvJ6-UIUrXz73iaOsWczH5Yg1ipvWnpOxgHGxNPb32g",
// 			"uid": "1",
//       	"username": "ken"
//     }
// }
//  @apiErrorExample {json} Error-Response:
//      HTTP/1.1 200 OK
//      {
//         "status_code": 400,
//         "message": "some reason",
//         "data": ""
//      }
//
func (c *UserController) Login() {
	// 数据校验
	vErr := validates.LoginRequestValidate(c.ctx)
	if vErr != nil {
		c.output.Error(vErr.Error())
		return
	}

	// 登录
	userServer := services.UserServer{}

	returnData, err := userServer.Login(c.ctx)
	if err == nil {
		c.output.Success(returnData)
	} else {
		c.output.Error(err.Error())
	}
}

// Logout 退出登录
func (c *UserController) Logout() {
	userServer := services.UserServer{}

	returnData, err := userServer.Logout(c.ctx)
	if err == nil {
		c.output.Success(returnData)
	} else {
		c.output.Error(err.Error())
	}
}
